package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;

import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;

import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseController {
    @Reference
    private ContractService contractService;
    @RequestMapping("/list")
    public String PageInfo(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "3")Integer pageSize){
        //创建条件对象
        ContractExample example=new ContractExample();
        //设置条件根据时间降序或者排序
           example.setOrderByClause("create_time desc");
           //根据企业id去查询所有
        ContractExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        /**
         * 细粒度权限控制：
         * 1.普通用户登陆，degree=4,只能查看自己创建的购销合同
         * 2.部门经理登陆，degree=3,查看部门创建的购销合同
         * 3.如果是大部门经理登陆，degree=2可以查看本部门以及所有子部门下的员工创建的购销合同
         */
        //1.获取登陆用户
        User user = getloginUser();
        PageInfo<Contract>pageInfo=null;
        //判断degree=?查看购销合同的id
        if (user.getDegree()==4){
            //普通员工登陆，只能查看自己的购销合同,根据id去查询购销合同
            criteria.andCreateByEqualTo(user.getId());
            pageInfo= contractService.findByPage(example,pageNum,pageSize);
        }else if (user.getDegree()==3){
            //部门经理登陆，可以查看自己以及部门下员工创建的购销合同
            criteria.andCreateDeptEqualTo(user.getDeptId());
            pageInfo= contractService.findByPage(example,pageNum,pageSize);

        }else  if(user.getDegree()==2){

            //大部门经理登陆，可以看到自己以及所有子孙部门下创建的购销合同
           pageInfo= contractService.selectByDeptId(user.getDeptId(),pageNum,pageSize);



        }
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/contract/contract-list";

    }



    //进入添加页面
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "cargo/contract/contract-add";
    }


    //进入修改页面，数据回显
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Contract serviceById = contractService.findById(id);
        request.setAttribute("contract",serviceById);

        return "cargo/contract/contract-update";
    }


    //添加和修改的保存代码
    @RequestMapping("/edit")
    public String add(Contract contract){
        //企业id和名称都要从登陆的用户哪儿获取
        contract.setCompanyId(getLoginCompanyId());
        contract.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(contract.getId())){

            //记录创建人，创建人所属部门
            contract.setCreateBy(getloginUser().getId());
            contract.setCreateDept(getloginUser().getDeptId());
            contractService.save(contract);
        }else {
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list";
    }




    //进入页面，数据删除
    @RequestMapping("/delete")
    public String delete(String id){
   contractService.delete(id);
        return "redirect:/cargo/contract/list";
    }



    //查看方法
    @RequestMapping("/toView")
    public String toView(String id){
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        return "cargo/contract/contract-view";
    }


    //提交方法
    @RequestMapping("/submit")
    public String submit(String id){
        //1.构造购销合同对象
        Contract contract =new Contract();
        //2.设置id
        contract.setId(id);
        contract.setState(1);
        contractService.update(contract);
        return "redirect:/cargo/contract/list";
    }




    //取消
    @RequestMapping("/cancel")
    public String cancel(String id){
        //1.构造购销合同对象
        Contract contract =new Contract();
        //2.设置id
        contract.setId(id);
        contract.setState(0);
        contractService.update(contract);
        return "redirect:/cargo/contract/list";
    }





}
